﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////

#pragma once

#include "IResponseHandler.h"


namespace gmepgcs
{
    template<typename TResult>
    class GjwResponseHandler : public IResponseHandler<TResult>
    {
    public:
        explicit GjwResponseHandler();
        virtual ~GjwResponseHandler();

    protected:
        virtual void HandleBody(const QJsonObject &body) override;
        virtual void HandleData(const QJsonValue &data) = 0;
    };


    template<typename TResult>
    GjwResponseHandler<TResult>::GjwResponseHandler()
    {
    }

    template<typename TResult>
    GjwResponseHandler<TResult>::~GjwResponseHandler()
    {
    }

    template<typename TResult>
    inline void GjwResponseHandler<TResult>::HandleBody(const QJsonObject &body)
    {
        QString code = body["code"].toString();
        QString message = body["message"].toString();
        if (code.trimmed().toLower() != "success")
        {
            QString qErrorMessage = QString("internal request error: custom code - %1, message - %2").arg(code).arg(message);
            IGcsLogger::Get()->Error(qErrorMessage);

            m_errorMessage = qErrorMessage.toStdWString();
            return;
        }

        QJsonValue data = body["data"];
        HandleData(data);
    }

}